package stars;

import gameAppLayer.AssetPortal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import modifiers.Feature;
import modifiers.ViewModifier;

import stars.enums.StarParameters;
import view.ViewModule;

import com.jme3.audio.AudioNode;
import com.jme3.material.Material;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Quad;
import com.jme3.scene.shape.Sphere;

import controls.RotationControl;

/*
 * Creates the visual representation of a single starModel object
 */
public class StarViewBuilder {
	
	public ViewModule buildViews(
			StarModel model,
			ArrayList<Feature> features,
			StarParameters starParams) {	
	
	
	
//	public ArrayList<Spatial> buildViews(
//			StarModel model,
//			ArrayList<Feature> features,			
//			StarParameters starParams) {
		
//		ArrayList<ViewModule> views = new ArrayList<ViewModule>();				
		
		String type = model.getType();	
		System.out.println("ViewBuilder Type: " + type);
		float displaySize = starParams.getParameter(type).getDisplaySize();
		Material detailMaterial;
		Material farMaterial;		
		Random rand = new Random();		
		
		Sphere detailStar;
		Quad farStar;
		
		//-------------------------------------------------------
		/*
		 * Each star contains a Node.  All viewable or audible objects relevant to the star are attached to or detached from this Node.
		 * 
		 * Viewable objects:
		 * 1) A flat, textured Quad object seen on the main map 
		 * 2) A 3D, textured Sphere object seen when the star is presented in system view.
		 * 
		 * Audible objects:
		 * 1) A click sound, played when the star is selected by the mouse cursor
		 * 
		 */		
		
		//1) Set up graphics		
		//Defaults to either Quiet or Average surface texture, ViewModifiers may make it Active
		if (rand.nextDouble() < 0.5) {
			detailMaterial = AssetPortal.getMaterial(type + "Quiet");
		}
		else {
			detailMaterial = AssetPortal.getMaterial(type + "Average");
		}
		
		detailStar = new Sphere(32,32, displaySize/2.0f); //radius of star = displaySize/2.0
		detailStar.setTextureMode(Sphere.TextureMode.Projected);
		Geometry detailGeometry = new Geometry("Detail", detailStar);
		detailGeometry.setMaterial(detailMaterial);
		detailGeometry.rotate(0.7854f, 0, 0); //rotate slightly onto side, to observe rotation better
		Vector2f location = model.getPhysicalDataModel().getLocation();
		detailGeometry.getLocalTranslation().set(new Vector3f(location.x, location.y, 0.0f));
		
		farStar = new Quad(displaySize, displaySize);
		Geometry farGeometry = new Geometry("Far", farStar);
		farMaterial = AssetPortal.getMaterial(type + "Far");		
		farGeometry.setMaterial(farMaterial);
		farGeometry.move(-displaySize/2.0f, -displaySize/2.0f, 0f);
		
		//2) Apply ViewModifiers
		HashMap<String, String> changes;
		ViewModifier viewMod;
		
		for (Feature feature: features) {
			viewMod = feature.getViewModifier();
			changes = viewMod.getChanges();
			
			//load from AssetPortal, apply (TBD)
		}
		
		//3) Set up audio
		AudioNode audioSelection = AssetPortal.getAudioNode("selection");
		
		//4) Put together into ViewModule to return
		Node detailNode = new Node("detailNode");
		detailNode.attachChild(detailGeometry);
		
		Node farNode = new Node("farNode");
		farNode.attachChild(farGeometry);
		farNode.attachChild(audioSelection);
		
		ViewModule module = new ViewModule();
		module.addNode(detailNode);
		module.addNode(farNode);
		
		return module;
	}
}